package com.hk.webapp.controller.client;

import com.hk.webapp.annotations.AllowAnonymous;
import com.hk.webapp.bean.CmsArticle;
import com.hk.webapp.service.CmsArticleService;
import com.hk.webapp.service.CmsCategoryService;
import com.hk.webapp.vo.*;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Maps;
import com.hk.webapp.vo.*;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

@RestController("ClientCmsArticleController")
public class CmsArticleController extends ClientBaseController {
    @Resource
    private CmsCategoryService cmsCategoryService;
    @Resource
    private CmsArticleService cmsArticleService;

    @ApiOperation("获取文章详情")
    @PostMapping("/Article/Detail")
    @AllowAnonymous
    public ResultVo detail(@RequestBody IdVo vo){
        if (vo.getId()<=0){
            return ResultVo.createByError("数据不存在或已删除");
        }
        CmsArticle cmsArticle= cmsArticleService.getEntityById(vo.getId());
        if (cmsArticle==null){
            return ResultVo.createByError("数据不存在或已删除");
        }
        HashMap<String,Object> map= Maps.newHashMap();
        map.put("id",cmsArticle.getId());
        map.put("title",cmsArticle.getTitle());
        map.put("sub_title",cmsArticle.getSubTitle());
        map.put("description",cmsArticle.getDescription());
        map.put("img_url",cmsArticle.getImgUrl());
        map.put("imgs_url",cmsArticle.getImgsUrl());
        map.put("source",cmsArticle.getSource());
        map.put("link_url",cmsArticle.getLinkUrl());
        map.put("content",cmsArticle.getContent());
        map.put("create_time",cmsArticle.getCreateTime());
        map.put("publish_time",cmsArticle.getPublishTime());
        map.put("video_url",cmsArticle.getVideoUrl());
        return ResultVo.createBySuccessData(map);
    }

    @ApiOperation("根据CODE值获取文章详情")
    @PostMapping("/Article/GetDetailByCode")
    @AllowAnonymous
    public ResultVo getDetailByCode(@RequestBody @Valid CodeVo vo){
        CmsArticle cmsArticle= cmsArticleService.getEntityByCode(vo.getCode(),0);
        if (cmsArticle==null){
            return ResultVo.createByError("数据不存在或已删除");
        }
        HashMap<String,Object> map= Maps.newHashMap();
        map.put("id",cmsArticle.getId());
        map.put("title",cmsArticle.getTitle());
        map.put("sub_title",cmsArticle.getSubTitle());
        map.put("description",cmsArticle.getDescription());
        map.put("img_url",cmsArticle.getImgUrl());
        map.put("imgs_url",cmsArticle.getImgsUrl());
        map.put("source",cmsArticle.getSource());
        map.put("link_url",cmsArticle.getLinkUrl());
        map.put("content",cmsArticle.getContent());
        map.put("create_time",cmsArticle.getCreateTime());
        map.put("publish_time",cmsArticle.getPublishTime());
        map.put("video_url",cmsArticle.getVideoUrl());
        return ResultVo.createBySuccessData(map);
    }

    @ApiOperation("获取文章列表")
    @PostMapping("/Article/GetList")
    @AllowAnonymous
    public ResultVo getList(@RequestBody CatePageVo vo){
        if (vo.getCate_id()<=0){
            return ResultVo.createByError("非法参数");
        }
        PageInfo<CmsArticle> plist=cmsArticleService.getPageListByCateIdAndState(vo.getPageIndex(),vo.getPageSize(),vo.getCate_id(),1);
        if (plist.getTotal()==0){
            return ResultVo.createBySuccessData(Arrays.asList(),plist.getTotal());
        }
        return ResultVo.createBySuccessData(plist.getList().parallelStream().map(c->{
            HashMap<String,Object> map=Maps.newHashMap();
            map.put("id",c.getId());
            map.put("category_id",c.getCategoryId());
            map.put("title",c.getTitle());
            map.put("sub_title",c.getSubTitle());
            map.put("bak_title",c.getBakTitle());
            map.put("description",c.getDescription());
            map.put("bak_description",c.getBakDescription());
            map.put("img_url",c.getImgUrl());
            map.put("imgs_url",c.getImgsUrl());
            map.put("link_url",c.getLinkUrl());
            map.put("video_url",c.getVideoUrl());
            map.put("attachment",c.getAttachment());
            map.put("create_time",c.getCreateTime());
            map.put("publish_time",c.getPublishTime());
            return map;
        }).collect(Collectors.toList()),plist.getTotal());
    }

    @ApiOperation("根据分类CODE值获取文章列表")
    @PostMapping("/Article/GetListByCode")
    @AllowAnonymous
    public ResultVo getListByCode(@RequestBody @Valid CodePageVo vo){
        PageInfo<CmsArticle> plist=cmsArticleService.getPageListByCodeAndState(vo.getPageIndex(),vo.getPageSize(),vo.getCode(),1);
        if (plist.getTotal()==0){
            return ResultVo.createBySuccessData(Arrays.asList(),plist.getTotal());
        }
        return ResultVo.createBySuccessData(plist.getList().parallelStream().map(c->{
            HashMap<String,Object> map=Maps.newHashMap();
            map.put("id",c.getId());
            map.put("category_id",c.getCategoryId());
            map.put("title",c.getTitle());
            map.put("sub_title",c.getSubTitle());
            map.put("bak_title",c.getBakTitle());
            map.put("description",c.getDescription());
            map.put("bak_description",c.getBakDescription());
            map.put("img_url",c.getImgUrl());
            map.put("imgs_url",c.getImgsUrl());
            map.put("link_url",c.getLinkUrl());
            map.put("video_url",c.getVideoUrl());
            map.put("attachment",c.getAttachment());
            map.put("create_time",c.getCreateTime());
            map.put("publish_time",c.getPublishTime());
            return map;
        }).collect(Collectors.toList()),plist.getTotal());
    }

    @ApiOperation("搜索")
    @PostMapping("/Article/Search")
    @AllowAnonymous
    public ResultVo Search(@RequestBody KeyPageVo vo){
        List<Integer> cates=Arrays.asList(2,3,4,5);
        PageInfo<CmsArticle> plist= cmsArticleService.searchPageListByKey(vo.getPageIndex(),vo.getPageSize(),cates,vo.getKey());
        if (plist.getTotal()==0){
            return ResultVo.createBySuccessData(Arrays.asList(),plist.getTotal());
        }
        return ResultVo.createBySuccessData(plist.getList().parallelStream().map(c->{
            HashMap<String,Object> map=Maps.newHashMap();
            map.put("id",c.getId());
            map.put("category_id",c.getCategoryId());
            map.put("title",c.getTitle());
            map.put("sub_title",c.getSubTitle());
            map.put("bak_title",c.getBakTitle());
            map.put("description",c.getDescription());
            map.put("bak_description",c.getBakDescription());
            map.put("img_url",c.getImgUrl());
            map.put("imgs_url",c.getImgsUrl());
            map.put("link_url",c.getLinkUrl());
            map.put("video_url",c.getVideoUrl());
            map.put("attachment",c.getAttachment());
            map.put("create_time",c.getCreateTime());
            map.put("publish_time",c.getPublishTime());
            return map;
        }).collect(Collectors.toList()),plist.getTotal());
    }
}
